package de.sonnentempel.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.hibernate.annotations.Immutable;

import de.sonnentempel.entity.Kunde;
import de.sonnentempel.framework.JPAUtil;

public class KundeDAOImpl implements KundeDAO {

	private DAOHelper helper;

	public KundeDAOImpl() {
		helper = new DAOHelper(Kunde.class);
	}

	public List<Kunde> getAll() throws Exception {
		EntityManager em = JPAUtil.getEntityManager();
		Query q = em.createQuery("select b from Benutzer b");
		List<Kunde> ret = q.getResultList();
		em.close();
		return ret;
	}

	public List<Kunde> getByVorname(String vorname) throws Exception {
		EntityManager em = JPAUtil.getEntityManager();
		Query q = em
				.createQuery("select k from Kunde k where k.vorname like '%"
						+ vorname + "%'");
		List<Kunde> ret = q.getResultList();
		em.close();
		return ret;
	}

	@Override
	public List<Kunde> getByNachname(String nachname) {
		EntityManager em = JPAUtil.getEntityManager();
		Query q = em
				.createQuery("select k from Kunde k where k.nachname like '%"
						+ nachname + "%'");
		List<Kunde> ret = q.getResultList();
		em.close();
		return ret;
	}

	// Die delegierten Methoden
	public Kunde getById(Long id) throws Exception {
		return (Kunde) helper.getById(id);
	}

	public Kunde save(Kunde o) throws Exception {
		return (Kunde) helper.save(o);
	}

	public Kunde update(Kunde o) throws Exception {
		return (Kunde) helper.update(o);

	}

	public void deleteById(Kunde o) throws Exception {
		helper.deleteById(o);
	}

}
